/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.tokenizers;

import java.io.IOException;
import java.util.Map;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;
public class PathHierarchyTokenizer extends ElasticTokenizer{
	
	
	public static final String DELIMITER_PARAM_KEY = "delimiter";
	public static final String REPLACEMENT_PARAM_KEY = "replacement";
	public static final String BUFFER_SIZE_PARAM_KEY = "buffer_size";
	public static final String REVERSE_PARAM_KEY = "reverse";
	public static final String SKIP_PARAM_KEY = "skip";
	
	
	Character delimiter;
	Character replacement;
	private Integer bufferSize;
	private Boolean reverse;
	private Integer skip;
	
	
	public PathHierarchyTokenizer(String defName) {
		this(null, defName);
	}

	public PathHierarchyTokenizer(Map<String,String> params, String defName) {
		super(ElasticTokenizerType.path_hierarchy, defName);
		
		if(params!=null){
	
			String delimStr = params.get(DELIMITER_PARAM_KEY);
			if(delimStr != null && !delimStr.isEmpty()){
				this.delimiter = delimStr.charAt(0);
			}
			
			String replStr = params.get(REPLACEMENT_PARAM_KEY);
			if(replStr != null && !replStr.isEmpty()){
				this.replacement = replStr.charAt(0);
			}
			
			String bufStr = params.get(BUFFER_SIZE_PARAM_KEY);
			if(bufStr != null && !bufStr.isEmpty()){
				try {
					int b = Integer.parseInt(bufStr);
					this.bufferSize = b;
				} catch (NumberFormatException e) {
				}
			}
			
			String revStr = params.get(REVERSE_PARAM_KEY);
			if(revStr != null && !revStr.isEmpty()){
				this.reverse = Boolean.parseBoolean(revStr);
			}
			
			String skipStr = params.get(SKIP_PARAM_KEY);
			if(skipStr != null && !skipStr.isEmpty()){
				try {
					int b = Integer.parseInt(skipStr);
					this.skip = b;
				} catch (NumberFormatException e) {
				}
			}
		}
	}
	
	
	@Override
	public boolean hasCustomParams() {
		 if(this.delimiter!= null && 
			this.replacement!= null && 
			this.bufferSize!= null && this.bufferSize!=256 &&
			this.reverse != null &&
			this.skip != null
		 )return true;
		 return false;
	}
	

	
	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefName())
		.field("type", getElasticTokenizerType().toString());
		
		if(this.delimiter!= null){
			mappingBuilder.field(DELIMITER_PARAM_KEY, this.delimiter.toString());
		}
		
		if(this.replacement != null){
			mappingBuilder.field(REPLACEMENT_PARAM_KEY, this.replacement.toString());				
		}
		
		if(this.bufferSize != null){
			mappingBuilder.field(BUFFER_SIZE_PARAM_KEY, this.bufferSize.intValue());
		}
		
		if(this.reverse != null){
			mappingBuilder.field(REVERSE_PARAM_KEY, this.reverse.booleanValue());			
		}
		
		if(this.skip != null){
			mappingBuilder.field(SKIP_PARAM_KEY, this.skip.intValue());				
		}
		
		return mappingBuilder;
	}

	@Override
	public void setParamsFromSettings(Settings tokenizerSettings) {
		
		String delimSet = tokenizerSettings.get(DELIMITER_PARAM_KEY);
		if(delimSet!=null && !delimSet.isEmpty()){
			this.delimiter = delimSet.charAt(0);
		}
		
		String replSet = tokenizerSettings.get(REPLACEMENT_PARAM_KEY);
		if(replSet!=null && !replSet.isEmpty()){
			this.replacement = replSet.charAt(0);
		}
		
		
		String bsizeSet = tokenizerSettings.get(BUFFER_SIZE_PARAM_KEY);
		if(bsizeSet!=null && !bsizeSet.isEmpty()){
			try {
				int mtl = Integer.parseInt(bsizeSet);
				this.bufferSize = mtl;
			} catch (NumberFormatException e) {
			}
		}
		
		
		String revSet = tokenizerSettings.get(REVERSE_PARAM_KEY);
		if(revSet!=null && !revSet.isEmpty()){
			this.reverse = Boolean.parseBoolean(revSet);
		}
		
		String skipSet = tokenizerSettings.get(SKIP_PARAM_KEY);
		if(skipSet!=null && !skipSet.isEmpty()){
			try {
				int b = Integer.parseInt(skipSet);
				this.skip = b;
			} catch (NumberFormatException e) {
			}
		}
	}
	
	
	
	
	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition otherTok){
		
		if(otherTok==null)return false;
		PathHierarchyTokenizer ot ;
		try {
			ot = (PathHierarchyTokenizer)otherTok;
		} catch (ClassCastException e) {
			return false;
		}
		return 
		EqualsUtil.areEqual(this.delimiter, ot.delimiter)
		&&
		EqualsUtil.areEqual(this.replacement, ot.replacement)
		&&
		EqualsUtil.areEqual(this.bufferSize, ot.bufferSize)
		&&
		EqualsUtil.areEqual(this.reverse, ot.reverse)
		&&
		EqualsUtil.areEqual(this.skip, ot.skip);
	}
	
	
	
	
	
	
	
	
}
